package com.example.query.controller;

import com.example.query.dto.QueryRequest;
import com.example.query.dto.QueryResponse;
import com.example.query.service.DynamicQueryService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/query")
@Tag(name = "动态查询接口", description = "提供通用的数据库查询功能")
public class DynamicQueryController {

    private final DynamicQueryService queryService;

    public DynamicQueryController(DynamicQueryService queryService) {
        this.queryService = queryService;
    }

    @Operation(summary = "执行动态查询", description = "根据请求参数动态查询指定表的数据")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "查询成功"),
        @ApiResponse(responseCode = "400", description = "请求参数错误"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping
    public QueryResponse query(
        @RequestBody @Valid QueryRequest request
    ) {
        return queryService.query(request);
    }
} 